package database;

import javax.swing.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

public class GUIdelete extends Component {
    JComboBox<String> attributeComboBox;
    JTextField valueTextField;
    JButton deleteButton;
    JFrame frame;
    Map<String, String> Mapping1,Mapping2,Mapping3,Mapping4,Mapping5;

    public GUIdelete(){
        frame=new JFrame();
        frame.setSize(400, 150);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(new GridLayout(3, 1));

    }

    public void GUIdeleteEmployee() {
        frame.setTitle("超市员工删除");
        Mapping1 = empolyeeMapping();
        // 选择员工属性
        JPanel attributePanel = new JPanel();
        attributePanel.setLayout(new FlowLayout());
        JLabel attributeLabel = new JLabel("选择员工属性:");
        String[] attributes = {"员工编号Eid", "员工姓名Ename", "员工级别Elevel", "员工电话EtelPhone", "员工薪水ESalary"}; // 属性选项
        attributeComboBox = new JComboBox<>(attributes);
        attributePanel.add(attributeLabel);
        attributePanel.add(attributeComboBox);

        // 填入数值
        JPanel valuePanel = new JPanel();
        valuePanel.setLayout(new FlowLayout());
        JLabel valueLabel = new JLabel("填入数值:");
        valueTextField = new JTextField(20); // 输入框大小
        valuePanel.add(valueLabel);
        valuePanel.add(valueTextField);

        // 添加按钮
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        deleteButton = new JButton("删除员工");
        deleteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String attribute1 = (String) attributeComboBox.getSelectedItem();
                String attribute2 = (String) Mapping1.get(attribute1);
                String value = valueTextField.getText();
                // 调用你的删除函数
                tb_employee e1 = new tb_employee();
                boolean deleted = e1.deleteEmployee(attribute2, value);
                if (deleted) {
                    JOptionPane.showMessageDialog(frame, "删除成功!");
                } else {
                    JOptionPane.showMessageDialog(frame, "删除失败或未找到符合条件的员工!");
                }
            }
        });
        buttonPanel.add(deleteButton);

        // 将三行添加到主窗口
        frame.add(attributePanel);
        frame.add(valuePanel);
        frame.add(buttonPanel);

        frame.setVisible(true);


    }



    public void GUIdeletegood(){
        frame.setTitle("超市商品删除");
        Mapping2= goodMapping();
        // 选择员工属性
        JPanel attributePanel = new JPanel();
        attributePanel.setLayout(new FlowLayout());
        JLabel attributeLabel = new JLabel("选择商品属性:");
        String[] attributes = {"商品编号Gid", "商品名称GName", "商品单价GPay", "客户编号Cid", "商品简介GIntroduction"}; // 属性选项
        attributeComboBox = new JComboBox<>(attributes);
        attributePanel.add(attributeLabel);
        attributePanel.add(attributeComboBox);

        // 填入数值
        JPanel valuePanel = new JPanel();
        valuePanel.setLayout(new FlowLayout());
        JLabel valueLabel = new JLabel("填入数值:");
        valueTextField = new JTextField(20); // 输入框大小
        valuePanel.add(valueLabel);
        valuePanel.add(valueTextField);

        // 添加按钮
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        deleteButton = new JButton("删除商品");
        deleteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String attribute1 = (String) attributeComboBox.getSelectedItem();
                String attribute2 = (String) Mapping2.get(attribute1);
                String value = valueTextField.getText();
                // 调用你的删除函数
                tb_employee e1 = new tb_employee();
                boolean deleted = e1.deleteEmployee(attribute2, value);
                if (deleted) {
                    JOptionPane.showMessageDialog(GUIdelete.this, "删除成功!");
                } else {
                    JOptionPane.showMessageDialog(GUIdelete.this, "删除失败或未找到符合条件的商品!");
                }
            }
        });
        buttonPanel.add(deleteButton);

        // 将三行添加到主窗口
        frame.add(attributePanel);
        frame.add(valuePanel);
        frame.add(buttonPanel);

        frame.setVisible(true);

    }

    public void GUIdeletecustomer(){
        frame.setTitle("超市客户删除");
        Mapping3 = customerMapping();
        // 选择员工属性
        JPanel attributePanel = new JPanel();
        attributePanel.setLayout(new FlowLayout());
        JLabel attributeLabel = new JLabel("选择客户属性:");
        String[] attributes = {"客户编号Cid", "客户名称CcompanyName", "商品简称CcompanySName", "地址CcompanyAddress", "公司电话CcompanyPhone","邮件Cemail","联系人Cname","联系人电话CtelPhone"}; // 属性选项
        attributeComboBox = new JComboBox<>(attributes);
        attributePanel.add(attributeLabel);
        attributePanel.add(attributeComboBox);

        // 填入数值
        JPanel valuePanel = new JPanel();
        valuePanel.setLayout(new FlowLayout());
        JLabel valueLabel = new JLabel("填入数值:");
        valueTextField = new JTextField(20); // 输入框大小
        valuePanel.add(valueLabel);
        valuePanel.add(valueTextField);

        // 添加按钮
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        deleteButton = new JButton("删除客户");
        deleteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String attribute1 = (String) attributeComboBox.getSelectedItem();
                String attribute2 = (String) Mapping3.get(attribute1);
                String value = valueTextField.getText();
                // 调用你的删除函数
                tb_customer c1 = new tb_customer();
                boolean deleted = c1.deleteCustomer(attribute2, value);
                if (deleted) {
                    JOptionPane.showMessageDialog(GUIdelete.this, "删除成功!");
                } else {
                    JOptionPane.showMessageDialog(GUIdelete.this, "删除失败或未找到符合条件的商品!");
                }
            }
        });
        buttonPanel.add(deleteButton);

        // 将三行添加到主窗口
        frame.add(attributePanel);
        frame.add(valuePanel);
        frame.add(buttonPanel);

        frame.setVisible(true);
    }
    public void GUIdeletepaydetail(){
        frame.setTitle("超市采购明细内容删除");
        Mapping4= paydetailMapping();
        // 选择员工属性
        JPanel attributePanel = new JPanel();
        attributePanel.setLayout(new FlowLayout());
        JLabel attributeLabel = new JLabel("选择客户属性:");
        String[] attributes = {"采购明细PDid", "采购清单Pid", "商品编号Gid", "采购数量Pcount", "商品单价GPay","商品总价total"}; // 属性选项
        attributeComboBox = new JComboBox<>(attributes);
        attributePanel.add(attributeLabel);
        attributePanel.add(attributeComboBox);

        // 填入数值
        JPanel valuePanel = new JPanel();
        valuePanel.setLayout(new FlowLayout());
        JLabel valueLabel = new JLabel("填入数值:");
        valueTextField = new JTextField(20); // 输入框大小
        valuePanel.add(valueLabel);
        valuePanel.add(valueTextField);

        // 添加按钮
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        deleteButton = new JButton("删除该采购明细");
        deleteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String attribute1 = (String) attributeComboBox.getSelectedItem();
                String attribute2 = (String) Mapping4.get(attribute1);
                String value = valueTextField.getText();
                // 调用你的删除函数
                tb_pay_detail d1 =new tb_pay_detail();
                boolean deleted = d1.deletePayDetail(attribute2, value);
                if (deleted) {
                    JOptionPane.showMessageDialog(GUIdelete.this, "删除成功!");
                } else {
                    JOptionPane.showMessageDialog(GUIdelete.this, "删除失败或未找到符合条件的采购明细!");
                }
            }
        });
        buttonPanel.add(deleteButton);

        // 将三行添加到主窗口
        frame.add(attributePanel);
        frame.add(valuePanel);
        frame.add(buttonPanel);

        frame.setVisible(true);
    }
    public void GUIdeletepaymain(){
        frame.setTitle("超市采购主表内容删除");
        Mapping5 = paymainMapping();
        // 选择员工属性
        JPanel attributePanel = new JPanel();
        attributePanel.setLayout(new FlowLayout());
        JLabel attributeLabel = new JLabel("选择客户属性:");
        String[] attributes = {"采购清单Pid", "员工编号Eid", "采购数量Pcount", "采购总价Ptotal", "采购时间Pdate"}; // 属性选项
        attributeComboBox = new JComboBox<>(attributes);
        attributePanel.add(attributeLabel);
        attributePanel.add(attributeComboBox);

        // 填入数值
        JPanel valuePanel = new JPanel();
        valuePanel.setLayout(new FlowLayout());
        JLabel valueLabel = new JLabel("填入数值:");
        valueTextField = new JTextField(20); // 输入框大小
        valuePanel.add(valueLabel);
        valuePanel.add(valueTextField);

        // 添加按钮
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout());
        deleteButton = new JButton("删除客户");
        deleteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String attribute1 = (String) attributeComboBox.getSelectedItem();
                String attribute2 = (String) Mapping5.get(attribute1);
                String value = valueTextField.getText();
                // 调用删除函数
                tb_pay_main m1=new tb_pay_main();
                boolean deleted = m1.deletePayMain(attribute2, value);
                if (deleted) {
                    JOptionPane.showMessageDialog(GUIdelete.this, "删除成功!");
                } else {
                    JOptionPane.showMessageDialog(GUIdelete.this, "删除失败或未找到符合条件的采购主表内容!");
                }
            }
        });
        buttonPanel.add(deleteButton);

        // 将三行添加到主窗口
        frame.add(attributePanel);
        frame.add(valuePanel);
        frame.add(buttonPanel);

        frame.setVisible(true);
    }
    private Map<String, String> empolyeeMapping() {
        Map<String, String> map = new HashMap<>();
        map.put( "员工编号Eid", "Eid");
        map.put("员工姓名Ename", "Ename");
        map.put("员工级别Elevel", "Elevel");
        map.put( "员工电话EtelPhone", "EtelPhone");
        map.put( "员工薪水ESalary", "ESalary");
        return map;
    }

    private Map<String, String> goodMapping() {
        Map<String, String> map = new HashMap<>();
        map.put("商品编号Gid", "Gid");
        map.put("商品名称GName", "GName");
        map.put("商品单价Gpay", "Gpay");
        map.put("商品编号Cid", "Cid");
        map.put("商品简介GIntroduction", "GIntroduction");
        return map;
    }
    private Map<String, String> customerMapping() {
        Map<String, String> map = new HashMap<>();
        map.put("客户编号Cid","Cid");
        map.put("客户名称CcompanyName","CcompanyName");
        map.put("商品简称CcompanySName","CcompanySName");
        map.put("地址CcompanyAddress","CcompanyAddress");
        map.put("公司电话CcompanyPhone","CcompanyPhone");
        map.put("邮件Cemail","Cemail");
        map.put("联系人Cname","Cname");
        map.put("联系人电话CtelPhone","CtelPhone");
        return map;
    }
    private Map<String, String> paymainMapping() {
        Map<String, String> map = new HashMap<>();
        map.put("采购清单Pid", "Pid");
        map.put("员工编号Eid", "Eid");
        map.put("采购数量Pcount", "Pcount");
        map.put("采购总价Ptotal", "Ptotal");
        map.put("采购时间Pdate", "Pdate");
        return map;
    }
    private Map<String, String> paydetailMapping() {
        Map<String, String> map = new HashMap<>();
        map.put("采购明细PDid", "PDid");
        map.put("采购清单Pid", "Pid");
        map.put("商品编号Gid", "Gid");
        map.put("采购数量Pcount", "Pcount");
        map.put("商品单价GPay", "GPay");
        map.put("商品总价total", "Total");
        return map;
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                GUIdelete d1=new GUIdelete();
                d1.GUIdeleteEmployee();
            }
        });
    }
}
